package com.j.thetabk.thetas;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

/**
 * Created by j on 2016/4/13.
 */
public class ThetaSConnectorC extends ThetaSConnectorB {


    public static String [] valueName={
            "_wlanChannel",//state
            "dateTimeZone",
            "gpsInfo",
            "remainingPictures",
            "remainingSpace",

            "_remainingVideos",
            "totalSpace",
            "_latestEnabledExposureDelayTime"
    };

    public static void AddValueName(JSONArray jainout, int [] indexes)
    {
        for (int indexe : indexes) jainout.put(valueName[indexe]);
    }

    public long totalSpace;
    public long _remainingVideos;
    public long remainingSpace;
    public long remainingPictures;

    public int _wlanChannel;
    public String dateTimeZone;
    public JSONObject gpsInfo;
    public long _latestEnabledExposureDelayTime;

    public ThetaSConnectorC(String cameraIpAddress) {
        super(cameraIpAddress);
    }

    protected void ParseValue(JSONObject options) throws JSONException {
        _wlanChannel=options.getInt(valueName[0]);
        dateTimeZone=options.getString(valueName[1]);
        gpsInfo=options.getJSONObject(valueName[2]);
        remainingPictures = options.getLong(valueName[3]);
        remainingSpace = options.getLong(valueName[4]);
        _remainingVideos=options.getLong(valueName[5]);
        totalSpace = options.getLong(valueName[6]);
        if(FirmwareVersionNumber()>=1.42)
        _latestEnabledExposureDelayTime = options.getLong(valueName[7]);
    }

    public boolean GetValue() throws IOException, JSONException {
        JSONArray jaProperties=new JSONArray();
        if(FirmwareVersionNumber()>=1.42)
            AddValueName(jaProperties,new int[]{0, 1, 2, 3, 4, 5, 6, 7}) ;
        else
            AddValueName(jaProperties,new int[]{0, 1, 2, 3, 4, 5, 6});
        JSONObject input=AcquirePropertyParam(jaProperties, sessionId);
        JSONObject output = TransmitA(3, input);
        if (IsStateDone(output)) {
            JSONObject options = GetProperties(output);
            ParseValue(options);
            return true;
        }
        return false;
    }

    public String TakePhoto(int sleepms) throws IOException, JSONException, InterruptedException {
        JSONObject input;
        input=ShootStillImageParam(sessionId);
        JSONObject output = TransmitA(3, input);
        String commandId = output.getString("id");
        while (!IsStateDone(output)) {
            Log.v("TakePhoto", output.toString());
            Thread.sleep(sleepms);
            input=CheckStatusParam(commandId);
            output =TransmitA(4,input);
        }
        Log.v("TakePhoto",output.toString());
        GetState();
        GetValue();
        return GetFileUri(output);
    }



    public static  String ToTime(long second) {
        String str = String.format("%02d", second % 60);
        if (second < 60)
            return "0:00:" + str;

        second /= 60;
        str = String.format("%02d", second % 60) + ":" + str;
        if (second < 60)
            return "0:" + str;

        second /= 60;
        str = String.valueOf(second) + ":" + str;
        return str;
    }

    public String StorageStr(int mode)
    {
             int megaByte = 1024 * 1024;
            String msgStr =  (int) (remainingSpace / megaByte) + "/" + (int) (totalSpace / megaByte) + "[MB] ";
            if(mode==0)
                return msgStr+remainingPictures;
         return msgStr+ToTime(_remainingVideos);
    }


    public int VideoRecordUpdate(boolean trigger, int waitms) throws IOException, JSONException, InterruptedException {
        GetState();
        if(trigger)
        {
            if (IsIdle())
            {
                StartVideo();
                Thread.sleep(waitms);
                return 1;//record start
            }
            StopVideo();
            Thread.sleep(waitms);
            return 2;//record end
        }
        if (IsIdle())
        {
//            if (lastRecordableTime == 0 && lastRecordedTime != 0)
//            {
//                StartVideo();
//                GetValue();
//                return 3;//record continue when pause due to file segment
//            }
            GetValue();
            Thread.sleep(waitms);
            return 4;//idle
        }

        if(_recordableTime>2)
        {
            Thread.sleep(waitms);
            return 0;//recording
        }

        while(!GetStateIdle())
        {
            //Log.v("VideoRecordUpdate","waiting");
        }
        StartVideo();
        GetState();
        GetValue();
        return 3;//record continue when pause due to file segment
    }

    public String RecordingMsg(long segCount)
    {
        String timeStr = ToTime((int) (_recordedTime + (segCount) * 1500)) + "/" + ToTime((int) _recordableTime);
        if (_cameraError != null) {
            for (String str : _cameraError)
                timeStr = timeStr + "\n" + str;
        }
        return timeStr;
    }

}
